package com.china.school.convert;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

import java.text.ParseException;
import java.util.Date;

public class MyDateConverter implements Converter<String, Date> {
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    @Override
    public Date convert(String source) {
        if (source == null)
        {
            return null;
        }
        try
        {
            return  DateUtils.parseDate(source.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }
}
